package com.ztom.v2;

/**
 * @author ZhangTao
 */
public class Code69ValidParentheses {

    public boolean isValid(String s) {
        if ((s.length() & 1) != 0) {
            return false;
        }
        char[] str = s.toCharArray();
        char[] stack = new char[s.length()];
        int index = -1;
        for (char c : str) {
            if (index == -1) {
                stack[++index] = c;
            } else if (isValid(stack[index], c)) {
                index--;
            } else {
                stack[++index] = c;
            }
        }
        return index == -1;
    }

    public boolean isValid(char a, char b) {
        return (a == '(' && b == ')') || (a == '[' && b == ']') || (a == '{' && b == '}');
    }
}
